package main

import (
	"flag"
	"fmt"
	"reflect"
)

func main() {
	//类型断言
	// 对于类型为interface的变量，用于判断类型
	var v interface{}
	fmt.Println("1", reflect.TypeOf(v)) // nil

	// 变量v 赋值为MyValue
	v = MyValue{value: "abc"}
	fmt.Println("2", reflect.TypeOf(v)) // main.MyValue

	getType(v)

	// 修改v的类型
	v = 1
	getType(v)
}

func getType(i interface{}) error {
	switch i.(type) {
	case int:
		fmt.Println("the type of int")
	case bool:
		fmt.Println("ther type of bool")
	case string:
		fmt.Println("the type of string")
	case float64:
		fmt.Println("the type of float64")
	case flag.Value: //不能断言实现的接口
		fmt.Println("the type of flag.Value")
	case MyValue:
		fmt.Println("the type of MyValue")
	default:
		fmt.Println("the type of an other type")
	}
	return nil
}

type MyValue struct {
	value string
}

func (m *MyValue) String() string {
	return m.value
}

func (m *MyValue) Set(s string) error {
	m.value = s
	return nil
}

func (m *MyValue) abc() {
}
